#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

using System.Web;

using System.Collections;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class PassportForm : Form
    {
        Passport m_passport;
        User m_current_user;
        EditMode m_editmode;
        PassportService m_passportService;
        MainForm m_mainForm;
        IPassportParentForm m_parentForm;

        public PassportForm(string name, Passport user, User currentUser, MainForm mainForm, IPassportParentForm parentForm, EditMode editmode)
        {
            InitializeComponent();
           
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
            m_parentForm = parentForm;
            m_passportService = (PassportService)ServiceFactory.Instance().GetService(ServiceFactory.PASSPORT_SERVICE);
            initButtonEvent();
            initComboDataSource();
            if (user.m_passportID == 0)
            {
                clearForm(user.m_passportNo);
            }
            else
            {
                m_passport = m_passportService.GetPassportByID(user.m_passportID);
                loadPassport(); 
                enableForm(editmode == EditMode.Edit);
            }
            setEditMode(editmode);
            //enableForm(false);
        }

        private void clearForm(string passportNo)
        {
            textBoxPassportNo.Text = passportNo;
            dateTimePickerIssueDate.Value = DateTime.Today;
            dateTimePickerExpireDate.Value = DateTime.Today;
            comboBoxCountry.SelectedIndex = 0;
            textBoxOfficeIssue.Text = "";

            textBoxName.Text = "";
            comboBoxGender.SelectedIndex = 0;
            textBoxBirthPlace.Text = "";
            dateTimePickerBirthDate.Value = DateTime.Today;
            //comboBoxNationality.SelectedIndex = 0;
            comboBoxNationality.Text = "INDONESIA";
            comboBoxCountry.Text = "INDONESIA";
            textBoxPlaceOfResidence.Text = "";
            textBoxOccupation.Text = "";
            comboBoxMaritalstatus.SelectedIndex = 0;
            textBoxAddress.Text = "";
            textBoxCity.Text = "";
            textBoxZipCode.Text = "";
            textBoxRemark.Text = "";
            textBoxIDnumber.Text = "";
            m_passport = new Passport();
            clearErrorProvider();
            enableForm(true);
            setEditMode(EditMode.Create);
            textBoxPassportNo.SelectAll();
        }

        private void initButtonEvent()
        {
            //toolBarButtonClear.Click += new EventHandler(toolBarButtonClear_Click);
            toolBarButtonSave.Click += new EventHandler(toolBarButtonSave_Click);
            buttonSAve.Click += new EventHandler(toolBarButtonSave_Click);
            //toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
            toolBarButtonEdit.Click += new EventHandler(toolBarButtonEdit_Click);
            //toolBarButtonSearch.Click += new EventHandler(toolBarButtonSearch_Click);
            toolBarButtonCancel.Click += new EventHandler(toolBarButtonCancel_Click);
        }
              
        void toolBarButtonEdit_Click(object sender, EventArgs e)
        {
            setEditMode(EditMode.Edit);
            enableForm(!toolBarButtonEdit.Enabled);
        }

        void toolBarButtonCancel_Click(object sender, EventArgs e)
        {
            if (m_editmode == EditMode.Load)
                this.Close();
            else
            MessageBox.Show("Are you sure to cancel?", "Confirmation", MessageBoxButtons.YesNo,
               MessageBoxIcon.Question, new EventHandler(cancelConfirmation));
        }

        void cancelConfirmation(object sender, EventArgs e)
        {
             MessageBoxWindow msg = (MessageBoxWindow)sender;
             if (msg.DialogResult == DialogResult.Yes)
             {
                 this.Close();
             }
        }
        private void initComboDataSource()
        {
            IList gender = new ArrayList();
            gender.Add("Male");
            gender.Add("Female");
            comboBoxGender.DataSource = gender;

            IList married = new ArrayList();
            married.Add("Married");
            married.Add("Single");
            comboBoxMaritalstatus.DataSource = married;

            comboBoxCountry.DataSource = m_passportService.LoadAllCountry();
            comboBoxNationality.DataSource = m_passportService.LoadAllCountry();

        }

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            //loadAllUser();
            initComboDataSource();
            if (m_editmode == EditMode.Load || m_editmode == EditMode.Edit)
                loadPassport();
        }

        private void clearErrorProvider()
        {
            errorProviderUser.SetError(textBoxPassportNo, "");
            errorProviderUser.SetError(textBoxName, "");
        }
        private bool isValidForm()
        {
            clearErrorProvider();
            refreshDOBDate();//refresh DOB year -- ernawan 08 04 2013
            bool valid_a = textBoxPassportNo.Text != string.Empty;
            bool valid_b = textBoxName.Text != string.Empty;
            bool valid_c = comboBoxCountry.Text != string.Empty;
            if (!valid_a) errorProviderUser.SetError(textBoxPassportNo, "User Code can not be empty");
            if (!valid_b) errorProviderUser.SetError(textBoxName, "User Name can not be empty");
            if (!valid_c) errorProviderUser.SetError(comboBoxCountry, "Country not be empty");
            return valid_a && valid_b && valid_c;
        }

        void toolBarButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (isValidForm())
                {
                    m_passport.m_passportNo = textBoxPassportNo.Text;
                    m_passport.m_issueDate = dateTimePickerIssueDate.Value;
                    m_passport.m_expireDate = dateTimePickerExpireDate.Value;
                    m_passport.m_country = comboBoxCountry.Text;
                    m_passport.m_officeIssue = textBoxOfficeIssue.Text;

                    m_passport.m_name = textBoxName.Text;
                    m_passport.m_gender = comboBoxGender.SelectedItem.ToString();
                    m_passport.m_birthPlace = textBoxBirthPlace.Text;
                    m_passport.m_birthDate = dateTimePickerBirthDate.Value;
                    m_passport.m_nationality = comboBoxNationality.Text;
                    m_passport.m_placeOfResidence = textBoxPlaceOfResidence.Text;
                    m_passport.m_occupation = textBoxOccupation.Text;
                    m_passport.m_maritalStatus = comboBoxMaritalstatus.SelectedItem.ToString();
                    m_passport.m_address = textBoxAddress.Text;
                    m_passport.m_city = textBoxCity.Text;
                    m_passport.m_zipcode = textBoxZipCode.Text;
                    m_passport.m_remark = textBoxRemark.Text;
                    m_passport.m_idnumber = textBoxIDnumber.Text;
                    if (m_passport.m_passportID > 0)
                    {
                        m_passport.m_modfctnBy = m_current_user.UserID;
                        m_passportService.UpdatePassport(m_passport);
                    }
                    else
                    {
                        m_passport.m_cretnBy = m_current_user.UserID;
                        m_passportService.InserPassport(m_passport);
                    }
                    m_parentForm.LoadAllPassport();
                    m_parentForm.LoadPassport(m_passport);
                    //  MessageBox.Show("Record has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    //MessageBox.Show("Record has been saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, new EventHandler(closeForm));
                    MessageBox.Show(null,"Record has been saved", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, new EventHandler(closeForm));
                    //clearForm();
                    //loadAllUser();
                    //setEditMode(EditMode.Saved);
                    // this.Close();
                }
                else
                {
                    textBoxPassportNo.SelectAll();
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }

        }

        void closeForm(object sender, EventArgs e)
        {
            this.Close();
            m_parentForm.SetActiveCursor();
        }

        private void setEditMode(EditMode editMode)
        {
            toolBarButtonSave.Enabled = (editMode == EditMode.Create || editMode == EditMode.Edit || editMode == EditMode.Saved || editMode == EditMode.Delete)&& m_current_user.RoleDetails(this.Name).NewFlag;
            buttonSAve.Enabled = toolBarButtonSave.Enabled;
            toolBarButtonEdit.Enabled = (editMode == EditMode.Load) && m_current_user.RoleDetails(this.Name).UpdateFlag;
            m_editmode = editMode;
            labelEditMode.Text = "[" + editMode + "]";
        }

        private void enableForm(bool enable)
        {
            textBoxPassportNo.ReadOnly = !enable;
            dateTimePickerIssueDate.Enabled = enable;
            dateTimePickerExpireDate.Enabled = enable;
            comboBoxCountry.Enabled = enable;
            textBoxOfficeIssue.ReadOnly = !enable;
            textBoxName.ReadOnly = !enable;
            comboBoxGender.Enabled = enable;
            textBoxBirthPlace.ReadOnly = !enable;
            dateTimePickerBirthDate.Enabled = enable;
            comboBoxNationality.Enabled = enable;
            textBoxPlaceOfResidence.ReadOnly = !enable;
            textBoxOccupation.ReadOnly = !enable;
            comboBoxMaritalstatus.Enabled = enable;
            textBoxAddress.ReadOnly = !enable;
            textBoxCity.ReadOnly = !enable;
            textBoxZipCode.ReadOnly = !enable;
            textBoxRemark.ReadOnly = !enable;
            textBoxIDnumber.ReadOnly = !enable;
            textBoxDOBDate.ReadOnly = !enable;
            textBoxDOBMonth.ReadOnly = !enable;
            textBoxDOBYear.ReadOnly = !enable;
            textBoxExpireDate.ReadOnly = !enable;
            textBoxExpireMonth.ReadOnly = !enable;
            textBoxExpireYear.ReadOnly = !enable;
            textBoxIssueDate.ReadOnly = !enable;
            textBoxIssueMonth.ReadOnly = !enable;
            textBoxIssueYear.ReadOnly = !enable;
        }

        private void loadPassport()
        {
            textBoxPassportNo.Text = m_passport.m_passportNo;
            dateTimePickerIssueDate.Value = m_passport.m_issueDate;
            dateTimePickerExpireDate.Value = m_passport.m_expireDate;
            comboBoxCountry.Text = m_passport.m_country;
            textBoxOfficeIssue.Text = m_passport.m_officeIssue;

            textBoxName.Text = m_passport.m_name;
            comboBoxGender.Text = m_passport.m_gender;
            textBoxBirthPlace.Text = m_passport.m_birthPlace;
            dateTimePickerBirthDate.Value = m_passport.m_birthDate;
            comboBoxNationality.Text = m_passport.m_nationality;
            textBoxPlaceOfResidence.Text = m_passport.m_placeOfResidence;
            textBoxOccupation.Text = m_passport.m_occupation;
            comboBoxMaritalstatus.Text = m_passport.m_maritalStatus;
            textBoxAddress.Text = m_passport.m_address;
            textBoxCity.Text = m_passport.m_city;
            textBoxZipCode.Text = m_passport.m_zipcode;
            textBoxRemark.Text = m_passport.m_remark;
            textBoxIDnumber.Text = m_passport.m_idnumber;

            textBoxIssueDate.Text = dateTimePickerIssueDate.Value.Day.ToString();
            textBoxIssueMonth.Text = dateTimePickerIssueDate.Value.Month.ToString();
            textBoxIssueYear.Text = dateTimePickerIssueDate.Value.Year.ToString();
            textBoxExpireDate.Text = dateTimePickerExpireDate.Value.Day.ToString();
            textBoxExpireMonth.Text = dateTimePickerExpireDate.Value.Month.ToString();
            textBoxExpireYear.Text = dateTimePickerExpireDate.Value.Year.ToString();
            textBoxDOBDate.Text = dateTimePickerBirthDate.Value.Day.ToString();
            textBoxDOBMonth.Text = dateTimePickerBirthDate.Value.Month.ToString();
            textBoxDOBYear.Text = dateTimePickerBirthDate.Value.Year.ToString();
        }

        private void dateTimePickerIssueDate_ValueChanged(object sender, EventArgs e)
        {
            //dateTimePickerExpireDate.Value = dateTimePickerIssueDate.Value.AddYears(5);
            textBoxIssueDate.Text = dateTimePickerIssueDate.Value.Day.ToString();
            textBoxIssueMonth.Text = dateTimePickerIssueDate.Value.Month.ToString();
            textBoxIssueYear.Text = dateTimePickerIssueDate.Value.Year.ToString();
        }

        private void PassportForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void PassportForm_Load(object sender, EventArgs e)
        {
            textBoxPassportNo.SelectAll();
            KeyEnterEventFire(this.Controls);
        }

        private void KeyEnterEventFire(Control.ControlCollection controls)
        {
            foreach (Control c in controls)
            {
                if (c is TextBox || c is ComboBox || c is DateTimePicker || c is Button)
                {
                    c.KeyPress += new KeyPressEventHandler(c_KeyPress);
                }
                KeyEnterEventFire(c.Controls);
            }
        }

        void c_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                Control cont = (Control)sender;
                this.SelectNextControl((Control)sender, true, true, true, true);
            }
        }
        private void textBoxYearIssue_Validating(object sender, CancelEventArgs e)
        {
            bool valid = false;
            TextBox tb = (TextBox)sender;
            try
            {
                int d = Convert.ToInt16(tb.Text);
                DateTime date = new DateTime(Convert.ToInt16(textBoxIssueYear.Text), Convert.ToInt16(textBoxIssueMonth.Text), Convert.ToInt16(textBoxIssueDate.Text));
                dateTimePickerIssueDate.Value = date;
                valid = true;
            }
            catch { }
            e.Cancel = !valid;
        }
        private void textBoxYearExpire_Validating(object sender, CancelEventArgs e)
        {
            bool valid = false;
            TextBox tb = (TextBox)sender;
            try
            {
                int d = Convert.ToInt16(tb.Text);
                DateTime date = new DateTime(Convert.ToInt16(textBoxExpireYear.Text), Convert.ToInt16(textBoxExpireMonth.Text), Convert.ToInt16(textBoxExpireDate.Text));
                dateTimePickerExpireDate.Value = date;
                valid = true;
            }
            catch { }
            e.Cancel = !valid;
        }
        private void textBoxYearDOB_Validating(object sender, CancelEventArgs e)
        {
            bool valid = false;
            //TextBox tb = (TextBox)sender;
            try
            {
                //int d = Convert.ToInt16(tb.Text);
                DateTime date = new DateTime(Convert.ToInt32(textBoxDOBYear.Text), Convert.ToInt16(textBoxDOBMonth.Text), Convert.ToInt16(textBoxDOBDate.Text));
                dateTimePickerBirthDate.Value = date;
                valid = true;
            }
            catch { }
            e.Cancel = !valid;
        }

        private void refreshDOBDate()
        {
            try
            {
                DateTime date = new DateTime(Convert.ToInt32(textBoxDOBYear.Text), Convert.ToInt16(textBoxDOBMonth.Text), Convert.ToInt16(textBoxDOBDate.Text));
                dateTimePickerBirthDate.Value = date;
                DateTime expDate = new DateTime(Convert.ToInt16(textBoxExpireYear.Text), Convert.ToInt16(textBoxExpireMonth.Text), Convert.ToInt16(textBoxExpireDate.Text));
                dateTimePickerExpireDate.Value = expDate;
                DateTime issueDate = new DateTime(Convert.ToInt16(textBoxIssueYear.Text), Convert.ToInt16(textBoxIssueMonth.Text), Convert.ToInt16(textBoxIssueDate.Text));
                dateTimePickerIssueDate.Value = issueDate;
            }
            catch { }
        }

        private void dateTimePickerExpireDate_ValueChanged(object sender, EventArgs e)
        {
            textBoxExpireDate.Text = dateTimePickerExpireDate.Value.Day.ToString();
            textBoxExpireMonth.Text = dateTimePickerExpireDate.Value.Month.ToString();
            textBoxExpireYear.Text = dateTimePickerExpireDate.Value.Year.ToString();
        }

        private void dateTimePickerBirthDate_ValueChanged(object sender, EventArgs e)
        {
            textBoxDOBDate.Text = dateTimePickerBirthDate.Value.Day.ToString();
            textBoxDOBMonth.Text = dateTimePickerBirthDate.Value.Month.ToString();
            textBoxDOBYear.Text = dateTimePickerBirthDate.Value.Year.ToString();
        }
        private void textBox_GotFocus(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            tb.SelectAll();
        }

        private void comboBoxCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxNationality.Text = comboBoxCountry.Text;
        }
    }
}